<?php

require_once('inc/Users.php');
$users = users::get_all();
$users_options = '';

?><!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Final Project</title>
  <link rel="stylesheet" href="css/style.css">
</head>
<body>
  
  <h1>User Select</h1>
  
  <?php
  
  // Build list of visible users, put it into the output buffer
  ob_start();
  echo '
  <dl>';
  while ($rs = $users->fetch_assoc()):
    $is_current = intval($_POST['user']) == $rs['id'];
    $users_options .= '<option value="' . $rs['id'] . '"' . ($is_current ? ' selected' : '') . '>' . $rs['FirstName'] . '</option>';
    
    // Show either the selected user or all users
    if (!isset($_POST['user']) || $is_current || intval($_POST['user']) == -1):
  ?>
    <dt><?php echo $rs['FirstName'] . ' ' . $rs['LastName']; ?></dt>
    <dd>Created at: <?php echo $rs['created_at']; ?></dd>
    <dd>Updated at: <?php echo $rs['updated_at']; ?></dd>
  <?php
    endif;
  endwhile;
  echo '
  </dl>';
  
  $list = ob_get_contents();
  ob_clean();
  
  // Build form
  ?>
  <form action="" method="post">
    <fieldset>
      <legend>Show User</legend>
      <select name="user">
        <option value="-1">All Users</option>
        <optgroup label="Users">
          <?php echo $users_options; ?>
        </optgroup>
      </select>
      <input type="submit" value="Select User">
    </fieldset>
  </form>
  
  <?php echo $list; ?>
</body>
</html>